package com.sskj.fca.ui.fragment;


import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.router.provider.LogoutProvider;
import com.sskj.fca.R;
import com.sskj.fca.presenter.UpdatePwdFragmentPresenter;

import butterknife.BindView;

@Route(path = RConfig.APP_FRAGMENT_UPDATE_PWD)
public class UpdatePwdFragment extends BaseFragment<UpdatePwdFragmentPresenter> {
    @BindView(R.id.et_old_pwd)
    EditText etOldPwd;
    @BindView(R.id.et_new_pwd)
    EditText etNewPwd;
    @BindView(R.id.et_new_pwd_again)
    EditText etNewPwdAgain;
    @BindView(R.id.btn_submit_new_pwd)
    Button btnSubmitNewPwd;

    @Override
    protected int getLayoutId() {
        return R.layout.app_fragment_update_pwd;
    }

    @Override
    public UpdatePwdFragmentPresenter getPresenter() {
        return new UpdatePwdFragmentPresenter();
    }

    @Override
    public void initView() {
        ClickUtil.click(btnSubmitNewPwd, () -> {
            if (TextUtils.isEmpty(etOldPwd.getText())) {
                ToastUtil.showShort("请输入旧密码");
                return;
            }
            if (!CheckUtil.isPayPwd(etOldPwd.getText().toString())) {
                ToastUtil.showShort("旧密码格式错误");
                return;
            }
            if (TextUtils.isEmpty(etNewPwd.getText())) {
                ToastUtil.showShort("请输入新密码");
                return;
            }
            if (!CheckUtil.isPayPwd(etNewPwd.getText().toString())) {
                ToastUtil.showShort("新密码格式错误");
                return;
            }
            if (TextUtils.isEmpty(etNewPwdAgain.getText())) {
                ToastUtil.showShort("请再次输入新密码");
                return;
            }
            if (!etNewPwd.getText().toString().equals(etNewPwdAgain.getText().toString())) {
                ToastUtil.showShort("两次密码输入不一致");
                return;
            }
            mPresenter.pwdReset(etOldPwd.getText().toString().trim()
                    , etNewPwd.getText().toString().trim()
                    , etNewPwdAgain.getText().toString().trim());
        });
    }

    public void changeSuccess() {
        LogoutProvider logoutProvider = ARouter.getInstance().navigation(LogoutProvider.class);
        logoutProvider.logout();
    }


}
